/*
 * ! ******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2002-2017 by Hitachi Vantara : http://www.pentaho.com
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ******************************************************************************/

package org.yzbdl.lanius.core.plugins;

import java.net.MalformedURLException;
import java.net.URL;

import org.pentaho.di.core.exception.KettlePluginException;
import org.yzbdl.lanius.util.LogUtil;
import org.yzbdl.lanius.core.constant.LocalDir;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 注解型Jar插件
 */
public class JarFileAnnotationPlugin {
    private URL jarUrl;

    private String relJarPath;

    private URL folderUrl;

    private String relFolderPath;

    private String className;

    public JarFileAnnotationPlugin() {
        super();
    }

    /**
     * @param jarFile
     * @param classFile
     * @param annotation
     */
    public JarFileAnnotationPlugin(String className, URL jarUrl, URL folderUrl) {
        this.className = className;
        this.jarUrl = jarUrl;
        this.folderUrl = folderUrl;
    }

    @Override
    public String toString() {
        return jarUrl.toString();
    }

    /**
     * @return the jarUrl
     * @throws KettlePluginException
     * @throws MalformedURLException
     */
    public URL jarUrl() throws MalformedURLException, KettlePluginException {
        if (jarUrl == null) {
            this.jarUrl = getAbsJarUrl();
        }
        return jarUrl;
    }

    public URL folderUrl() throws MalformedURLException, KettlePluginException {
        if (folderUrl == null) {
            this.folderUrl = getAbsFolderUrl();
        }
        return folderUrl;
    }

    public String getRelJarPath() {
        if (StrUtil.isBlank(relJarPath)) {
            this.relJarPath = jarUrl.getPath().substring("///".length() + LocalDir.BASE.length());
        }
        return relJarPath;
    }

    public void setRelJarPath(String relJarPath) {
        this.relJarPath = relJarPath;
    }

    public String getRelFolderPath() {
        if (StrUtil.isBlank(relFolderPath)) {
            this.relFolderPath = folderUrl.getPath().substring("///".length() + LocalDir.BASE.length());
        }
        return relFolderPath;
    }

    public void setRelFolderPath(String relFolderPath) {
        this.relFolderPath = relFolderPath;
    }

    public URL getAbsJarUrl() throws MalformedURLException, KettlePluginException {
        String url = "file:////" + LocalDir.BASE + "/" + this.getRelJarPath();
        if (!FileUtil.exist(url)) {
            LogUtil.warn("文件[{}]不存在", url);
            throw new KettlePluginException("relJarPath所指文件不存在");
        }
        return new URL(url);
    }

    public URL getAbsFolderUrl() throws MalformedURLException, KettlePluginException {
        String url = "file:////" + LocalDir.BASE + "/" + this.getRelFolderPath();
        if (!FileUtil.exist(url)) {
            LogUtil.warn("文件[{}]不存在", url);
            throw new KettlePluginException("relFolderPath所指文件不存在");
        }
        return new URL(url);
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

}
